<template>
  <div id="websocket">
    <a-button type="primary" @click="initWebsocket">连接websocket</a-button>
  </div>
</template>

<script>
import { CreateWebsocket } from "@/utils/websocket";
export default {
  name: "Websocket",
  data() {
    return {
      ws: null,
    };
  },
  mounted() {
    this.initWebsocket();
  },
  beforeDestroy() {
    this.ws && this.ws.ondestory();
  },
  methods: {
    initWebsocket() {
      let options = {
        urlOption: {
          // protocol: 'wss://',
          // host: 'lqhan.dev.bigquant.ai',
          prefix: "/api",
          path: "/calcworker/v1/ws",
        },

        reconnect: false,
        maxReconnectNum: 5,
        reconnectDelay: 2000,

        openHeartbeat: false,
        heartbeatOptions: {
          heartbeatTime: 30000,
          sendData: {
            contentType: "hert",
          },
          closeTime: 2000,
        },
      };
      try {
        this.ws = new CreateWebsocket("0001", options);
        if (this.ws) {
          this.ws.onopen(() => {
            console.log("websocket连接成功");
          });
          this.ws.onerror((error) => {
            console.log("websocket连接错误", error);
          });
          this.ws.onclose((data) => {
            console.log("websocket连接关闭", data);
          });
          this.ws.onmessage((res) => {
            console.log("websocket消息", res);
          });
        }
      } catch (err) {
        console.log(err);
      }
    },
  },
};
</script>

<style lang="less"></style>
